//
//  ViewController.m
//  PaymentEngineDemo
//
//  Created by jay lei on 1/17/17.
//  Copyright © 2017 Jay Lei. All rights reserved.
//

#import "MViewController.h"

@implementation MViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
  
//    [self setUpBtn :startTransBtn];
//    [self setUpBtn :getDeviceBtn];
//    [self setUpBtn :getReceiptBtn];
//    [self setUpBtn :captureSignatureBtn];
//    [self setUpBtn :voidTransBtn];
//    [self setUpBtn :adjustTransaction];

}

-(void) inital{
  middlewareSetting = [PaymentEngineMiddlewareSettings getInstance];
  middlewareSetting.isTipAdjustSupported = true;
  middleware = [PaymentEngineMiddleware getInstance];
  
  /**
   * The mode can be either sandbox or production
   * If developer wants to test on our sandbox server, they will set the currentMode to sandbox
   * It's not case sensitive
   */
  middlewareSetting.currentMode = @"sandbox";
  middlewareSetting.sourceKey = @"_iOunDQQLRKNta66hjJhfBNiD1WU6EV7";
  middlewareSetting.pinNum = @"123456";
  
  /**
   * The below setting values are optional, by default they are set to false
   * This is used to print out the middleware debugging log during development
   */
  middlewareSetting.enableDebugLog = true;
  //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateResultLog:) name:MiddlewareLogNotification object:nil];
  //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userFlowNotif:) name:MiddlewareFlowNotification object:nil];
  
  /**
   * Initialize the middleware class with the device we will be using
   */
  [middleware setDevice:@"castle" :self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)setUpBtn :(UIButton *)btn
{
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.layer.cornerRadius = 8.0f;
    btn.backgroundColor = [UIColor colorWithRed:60.0f/255.0f green:114.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
}

- (void)startTransaction
{
    /**
     * Check to see if the terminal is connected
     * Alert the user if the terminal is not connected
     */
//    if([middleware isDeviceOnline])
//    {
        //Creates an empty dictionary objects to hold all the transaction info
        NSMutableDictionary *transDict = [NSMutableDictionary new];
        
        /**
         * Sets the basic info that is needed to start a transaction
         */
        [transDict setObject:@"cc:sale" forKey:@"command"];
        [transDict setObject:@"0.01" forKey:@"amount"];
        [transDict setObject:@"10000" forKey:@"invoice"];
        [transDict setObject:@"This is my first middleware transaction" forKey:@"description"];

        //Use the startTransaction method from the UsaepayMiddleware class to start the transaction.
        [middleware startTransaction:transDict];
    
}

-(void)readDeviceInfoComplete:(NSDictionary *)deviceInfoDict
{
    NSLog(@"read device completed: %@", deviceInfoDict);
}

/**
 * Delegate method for startTransaction, it gets call once the transaction is completed
 * @param transResponse - Holds all the response result from the gateway
 */
-(void)transactionComplete :(PaymentEngineTransactionResponse *)transResponse
{
    NSLog(@"Transaction Result: %@", transResponse.Result);
    NSLog(@"Transaction Status: %@", transResponse.Status);
    NSLog(@"Transaction Error: %@", transResponse.Error);
    NSLog(@"Expiration: %@", transResponse.expirationDate);
    NSLog(@"Transaction Type: %@", transResponse.transactionType);
    NSLog(@"transaction cardholder name: %@", transResponse.cardHolderName);
    
//    UIAlertController *terminalAlert = [UIAlertController alertControllerWithTitle:@"Status" message:transResponse.Status preferredStyle:UIAlertControllerStyleAlert];
//
//    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
//                               {
//                                   [self dismissViewControllerAnimated:YES completion:nil];
//                               }];
//
//    [terminalAlert addAction:okAction];
//
//    [self presentViewController:terminalAlert animated:YES completion:nil];
}

/**
 * Delegate method for getReceipt , it gets called once the EMV receipt is returned from the gateway
 * @param receiptValue - Holds all the necessary data for EMV receipt
 */
-(void)returnReceiptCompleted :(NSString *)receiptValue
{
    NSLog(@"EMV receipt value: %@", receiptValue);
}

///**
// * Delegate method for captureSignature , it gets call once we finish capturing the signature
// * @param transResponse - Holds all the response result from the gateway
// */
//-(void)captureSignatureComplete :(PaymentEngineTransactionResponse *)transResponse
//{
//
//    if(transResponse != nil)
//    {
//
//        UIAlertController *terminalAlert = [UIAlertController alertControllerWithTitle:@"Status" message:transResponse.Error preferredStyle:UIAlertControllerStyleAlert];
//
//        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
//                                   {
//                                       [self dismissViewControllerAnimated:YES completion:nil];
//                                   }];
//
//        [terminalAlert addAction:okAction];
//
//        [self presentViewController:terminalAlert animated:YES completion:nil];
//    }
//
//}
//
//
//-(void)adjustTranstionComplete:(PaymentEngineTransactionResponse *)transResponse
//{
//    if(transResponse != nil)
//    {
//
//        UIAlertController *terminalAlert = [UIAlertController alertControllerWithTitle:@"Status" message:transResponse.Status preferredStyle:UIAlertControllerStyleAlert];
//
//        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
//                                   {
//                                       [self dismissViewControllerAnimated:YES completion:nil];
//                                   }];
//
//        [terminalAlert addAction:okAction];
//
//        [self presentViewController:terminalAlert animated:YES completion:nil];
//    }
//}

/**
 * The delegate notification method prints out all the middleware debugging logs
 */
-(void)updateResultLog :(NSNotification *)notif
{
    
    NSString *logString = [notif object];
    
    NSLog(@"result log: %@", logString);

}

/**
 * The delegate notification method prints out the transaction flow logs
 */
-(void)userFlowNotif :(NSNotification *)notif
{
    NSString *userFlowLog = [notif object];
    
    NSLog(@"User flow log: %@", userFlowLog);

}

/**
 * Button Events
 */
//-(IBAction)getDeviceInfoEvent:(id)sender
//{
//    [middleware readDeviceInfo];
//}
//
//- (IBAction)  getReceiptEvent:(id)sender
//{
//    /**
//     * 1495713081 - It's a test Transaction ID
//     * The value is obtained from PaymentEngineTransactionResponse after transaction is completed
//     */
//    NSDictionary *receiptDict = [NSDictionary dictionaryWithObjectsAndKeys:@"1495713081", @"refnum",  nil];
//
//    [middleware getReceipt:receiptDict];
//}
//
//- (IBAction)  captureSignatureEvent:(id)sender
//{
//    UIImage *signatureImg = [UIImage imageNamed:@"SignatureImg.png"];
//    NSData *imgData = UIImageJPEGRepresentation(signatureImg, 0.5f);
//    NSString *imgBase64 = [imgData base64EncodedStringWithOptions:0];
//
//    NSMutableDictionary *transDict = [NSMutableDictionary new];
//
//    [transDict setObject:imgBase64 forKey:@"signature"];
//
//    /**
//     * Reference number of the the transaction that we want to capture the signature for
//     * Below is a test reference number
//     */
//    [transDict setObject:@"1537411846" forKey:@"refNum"];
//
//    /**
//     * The amount of the transaction
//     * This amount needs to match the reference number amount
//     * If amount is different, we will get an error capturing the signature
//     */
//    [transDict setObject:@"40" forKey:@"amount"];
//    [transDict setObject:@"capture" forKey:@"command"];
//
//    [middleware captureSignature:transDict];
//}
//
//- (IBAction)voidTransEvent:(id)sender {
//
//   UIAlertController *terminalAlert = [UIAlertController alertControllerWithTitle:@"Void Transaction" message:@"Enter transaction reference number" preferredStyle:UIAlertControllerStyleAlert];
//
//    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
//                               {
//                                   UITextField *alertTxtField = terminalAlert.textFields.firstObject;
//
//                                   if(alertTxtField.text.length > 0)
//                                   {
//                                       [self dismissViewControllerAnimated:YES completion:nil];
//
//                                       NSMutableDictionary *transDict = [NSMutableDictionary new];
//
//                                       [transDict setObject:@"cc:void" forKey:@"command"];
//                                       [transDict setObject:alertTxtField.text forKey:@"refnum"];
//
//                                       [middleware startTransaction:transDict];
//                                   }
//                               }];
//
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
//                                                   handler: nil];
//
//    [terminalAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//
//        textField.keyboardType = UIKeyboardTypeNumberPad;
//        textField.placeholder = @"Reference Number";
//
//    }];
//
//    [terminalAlert addAction:okAction];
//    [terminalAlert addAction:cancelAction];
//
//    [self presentViewController:terminalAlert animated:YES completion:nil];
//
//
//
//}
//
///**
// * Delegate method adjustTranstionComplete will get called once we finish adjusting the transaction
// */
//- (IBAction) adjustTransEvent:(id)sender
//{
//
//    NSMutableDictionary *transDict = [NSMutableDictionary new];
//
//    /**
//     * Reference number of the the transaction that we want to adjust
//     * Below is a test reference number
//     */
//    [transDict setObject:@"1919214943" forKey:@"refNum"];
//
//    //total amount including tip
//    [transDict setObject:@"50" forKey:@"amount"];
//    [transDict setObject:@"10" forKey:@"tip"];
//    [transDict setObject:@"adjust" forKey:@"command"];
//
//    [middleware adjustTransaction:transDict];
//}

@end
